// Copyright (C)2012 Johann Duscher. All rights reserved.
//
// This file is part of QwTAPI.
//
// QwTAPI is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// QwTAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with QwTAPI.  If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "stdafx.h"


namespace qwtapi
{

class Connection;

class QWTAPI_EXPORT Request : public QObject
{
    Q_OBJECT

    typedef QObject super;

    friend class Connection;

public:
    typedef const void* Type;

    ~Request();

    Type getRequestType() const;

    void setRequestId(LONG requestId);
    LONG getRequestId() const;

    bool isPending() const;
    bool isTimeout() const;
    LONG getResult() const;

    HCALL& callHandle();
    const HCALL& callHandle() const;

public slots:
    void onRequestComplete(LONG result);

signals:
    void requestComplete(const Request* pRequest);

private slots:
    void onTimeout();

private:
    Type m_requestType;
    LONG m_requestId;
    QTimer m_timeoutTimer;
    DWORD m_completeTime;
    bool m_timeout;

    union
    {
        HCALL m_hCall;
    };

    volatile LONG m_result;
    volatile bool m_pending;

    Request(Connection* pConnection);

    void initialize(Type requestType, LONG requestId, int timeout); // Called by Connection class.
    void startTimeoutTimer(); // Called by Connection class.
    void stopTimeoutTimer(); // Called by Connection class.
};

}